﻿#include "GUIInitialFieldTypeDialog.h"
#include "ui_GUIInitialFieldTypeDialog.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossCase.h"
#include "FITK_Component/FITKRadiossData/FITKInitialField.h"
#include <QStandardItemModel>
#include <QMessageBox>

namespace GUI
{
    GUIInitialFieldTypeDialog::GUIInitialFieldTypeDialog(QWidget* parent): QDialog(parent)
    {
        _ui = new Ui::GUIInitialFieldTypeDialog;
        _ui->setupUi(this);

        // 初始化对话框
        Qt::WindowFlags flags = windowFlags();
        flags &= ~Qt::WindowContextHelpButtonHint;
        setWindowFlags(flags);
        setMinimumSize(0, 0); // 允许窗口任意缩小
        adjustSize(); // 根据内容调整尺寸

        _ui->comboBoxType->addItem(tr("TRA"), 0);
        _ui->comboBoxType->addItem(tr("ROT"), 1);
        _ui->comboBoxType->addItem(tr("TG"), 2);
        _ui->comboBoxType->addItem(tr("GRID"), 3);
        // 获取ComboBox的模型，设置第二到第四项为不可选中
        QStandardItemModel* model = qobject_cast<QStandardItemModel*>(_ui->comboBoxType->model());
        if (model) {
            // 索引1对应第二项，索引3对应第四项
            for (int i = 1; i <= 3; ++i) {
                QStandardItem* item = model->item(i);
                if (item) {
                    // 清除ItemIsEnabled标志，使项目不可选中
                    item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
                }
            }
        }

        // 获取管理器
        Radioss::FITKInitialFieldManager* manager = getManagerInitialField();
        if (manager == nullptr) return;
        
        // 设置名称
        _ui->lineEditName->setText(manager->checkName("InitialField-1"));
        // 设置关键字
        _ui->lineEditKeyWord->setText(Radioss::FITKInitialFieldTra::GetFITKInitialFieldTraRadiossKeyWord());
        _ui->lineEditKeyWord->setReadOnly(true);
    }

    GUIInitialFieldTypeDialog::~GUIInitialFieldTypeDialog()
    {
        if (_ui != nullptr) delete _ui;
        _ui = nullptr;
    }

    QString GUIInitialFieldTypeDialog::getDataName()
    {
        return _ui->lineEditName->text();
    }

    int GUIInitialFieldTypeDialog::getDataType()
    {
        return _ui->comboBoxType->currentIndex();
    }

    QString GUIInitialFieldTypeDialog::getDataKeyWord()
    {
        return _ui->lineEditKeyWord->text();
    }

    void GUIInitialFieldTypeDialog::on_buttonOK_clicked()
    {
        // 获取管理器
        Radioss::FITKInitialFieldManager* manager = getManagerInitialField();
        if (manager == nullptr) return;

        // 获取名称
        QString name = _ui->lineEditName->text();
        if (name.isEmpty())
        {
            QMessageBox::warning(this, "Warning", tr("Name cannot be empty."), QMessageBox::Ok);
            return;
        }
        else if (manager->getDataByName(name) != nullptr)
        {
            QMessageBox::warning(this, "Warning", tr("\"%1\" The name already exists").arg(name), QMessageBox::Ok);
            return;
        }
        else this->accept();
    }

    void GUIInitialFieldTypeDialog::on_buttonCancel_clicked()
    {
        this->reject();
    }

    void GUIInitialFieldTypeDialog::on_comboBoxType_currentIndexChanged(int index)
    {
        if (index == 0) _ui->lineEditKeyWord->setText(Radioss::FITKInitialFieldTra::GetFITKInitialFieldTraRadiossKeyWord());
        else _ui->lineEditKeyWord->setText("");
    }

    Radioss::FITKInitialFieldManager* GUIInitialFieldTypeDialog::getManagerInitialField()
    {
        Radioss::FITKRadiossCase* dataCase = FITKAPP->getGlobalData()->getPhysicsData<Radioss::FITKRadiossCase>();
        return dataCase == nullptr ? nullptr : dataCase->getInitialFieldManager();
    }
}